// 指令查询查询条件组件
import { connect } from 'dva';
import moment from 'moment';
import React from 'react';

import { DATA_ENTRY, AdvancedSearch, codeType } from '../../../comps';
import { isZiGuanEvir } from '../../../utils/commonFunc';
import investmentTypeData from '../../../utils/enumeratedValues/investmentType';
import TtatusData from '../../../utils/enumeratedValues/statusData';

@connect(({ tradeManageM }) => ({ tradeManageM }))
class QueryC extends React.Component {
  // eslint-disable-next-line react/state-in-constructor
  state = {
    businessType: -1, // 当前选中的业务类型
    directionDisable: true, // 交易方向是否可点击
    isProducts: false, // 判断是否是生产环境，默认不是
    isZiGuan: false,
    userId: '',
    businessTypeData: undefined,
    businessTypeChild: undefined,
  };

  componentDidMount() {
    this.props.onRef(this);
  }

  // 响应业务类型变更
  businessTypeChanged = (e) => {
    if (e) {
      if (Array.isArray(e)) {
        this.setState({ businessType: -1, directionDisable: true, businessTypeData: e });
      } else {
        this.setState({ businessType: e, directionDisable: false, businessTypeData: undefined });
      }
    } else {
      this.setState({ businessType: -1, directionDisable: true, businessTypeData: undefined });
    }
  };

  // 响应查询
  onQuery = (query) => {
    // 证券代码组件会带出exchangeCode，本接口不需要，删除
    delete query.exchangeCode;
    // 日期格式转换
    query.search_GTE_createdTs = query.search_GTE_createdTs ? query.search_GTE_createdTs.format('YYYY-MM-DD') : '';
    query.search_LTE_createdTs = query.search_LTE_createdTs ? query.search_LTE_createdTs.format('YYYY-MM-DD') : '';
    if (query.search_EQ_type) {
      if (Array.isArray(this.state.businessTypeData)) {
        query.search_IN_type = this.state.businessTypeData.join(';');
        delete query.search_EQ_type;
      }
    }
    // 查询单个指令状态用search_EQ_busStatus， 多个search_IN_busStatus
    let queryItems = null;
    if (query.search_EQ_busStatus && query.search_EQ_busStatus.length === 1) {
      queryItems = {
        ...query,
        // 指令查询中“指令状态”更改为可多选  为非引用对象时无join
        search_EQ_busStatus: query.search_EQ_busStatus?.join(';'),
        search_NULL_method: '1',
        userId: localStorage.getItem('antd-pro-userId'),
      };
    } else {
      queryItems = {
        ...query,
        // 指令查询中“指令状态”更改为可多选  为非引用对象时无join
        search_IN_busStatus: query.search_EQ_busStatus?.join(';'),
        search_NULL_method: '1',
        userId: localStorage.getItem('antd-pro-userId'),
      };
      delete queryItems.search_EQ_busStatus;
    }
    // 抛出查询事件
    this.props.onQuery(queryItems);
  };

  // 响应重置
  onClear = () => {
    // 抛出重置事件
    this.props.onClear();
    this.setState({ businessType: -1, directionDisable: true, businessTypeData: undefined });
    this.state.businessTypeChild.clear();
  };

  tradingCodeChange = (e) => {
    if (e) {
      this.state.tradingCodeSelect.setSelect(e.exchangeCode);
      this.setState({
        securityExchangeData: e.exchangeCode,
      });
    }
  };

  render() {
    const dataSource = [
      {
        dataIndex: ['search_GTE_createdTs', 'search_LTE_createdTs'],
        title: '日期区间',
        type: DATA_ENTRY.RANGEDATE,
        initialValue: [moment().subtract(1, 'months'), moment()],
        allowClear: false,
        onRef: (ref) => {
          this.state.RANGEDATERef = ref;
        },
      },
      {
        dataIndex: 'portfolioId',
        title: '产品组合',
        multiple: true,
        type: DATA_ENTRY.PRODUCTGROUPTREE,
        treeCheckable: !!isZiGuanEvir(),
        tradeModel: ['BOOKKEEPING_MODE', 'COMMAND_MODE'], // 不包含估值解析和估值对接的产品
      },
      {
        dataIndex: 'search_EQ_type', // 业务类型
        type: DATA_ENTRY.BUSINESSTYPE,
        appType: '0',
        onChange: (e) => this.businessTypeChanged(e),
        onRef: (ref) => {
          this.state.businessTypeChild = ref;
        },
      },
      {
        dataIndex: 'search_EQ_direction',
        title: '交易方向',
        businessType: this.state.businessType,
        type: DATA_ENTRY.TRADINGDIRECT,
        disabled: this.state.directionDisable,
      },
      {
        dataIndex: 'search_LIKE_stockCode',
        title: '证券代码',
        type: DATA_ENTRY.SECURITIESCODE,
        codeType: codeType.positioned,
        onChange: (e) => this.tradingCodeChange(e),
      },
      {
        dataIndex: 'search_EQ_tradingMarket',
        title: '交易市场',
        type: DATA_ENTRY.TRADINGMARKET,
        marketType: ['jys', 'yhj', 'cw', 'ggt'],
        onRef: (ref) => {
          this.state.tradingCodeSelect = ref;
        },
      },
      {
        dataIndex: 'search_EQ_busStatus',
        title: '指令状态',
        type: DATA_ENTRY.SELECT,
        isMultiple: true,
        allowClear: true,
        dataSource: TtatusData.busStatus,
      },
      {
        dataIndex: 'search_EQ_riskStatus',
        title: '风控状态',
        type: DATA_ENTRY.SELECT,
        allowClear: true,
        dataSource: TtatusData.riskStatus,
      },
      {
        dataIndex: 'search_EQ_clinchStatus',
        title: '成交状态',
        type: DATA_ENTRY.SELECT,
        allowClear: true,
        dataSource: TtatusData.clinchStatus,
      },
      {
        dataIndex: 'search_EQ_payStatus',
        title: '支付状态',
        type: DATA_ENTRY.SELECT,
        allowClear: true,
        dataSource: TtatusData.payStatus,
      },

      {
        dataIndex: 'search_EQ_investmentType',
        title: '投资类型',
        type: DATA_ENTRY.SELECT,
        allowClear: true,
        dataSource: investmentTypeData,
      },
    ];

    this.state.isZiGuan && dataSource.pop();
    return <AdvancedSearch dataSource={dataSource} onQuery={this.onQuery} onClear={this.onClear} />;
  }
}

export default QueryC;
